package com.ophiux.wechatpay.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.io.IOUtils;

/**
 * 说明：HTTP请求  -- 模拟SoapUi调用<br>
 * <h1>创 建 人: hehailong</h1>
 * 创建日期: 2019年8月5日 下午1:38:12<br>
 * 修改记录：
 */
public class HisRouteUtil {

	public static void main(String[] args) {
		invokeSoapUI("http://hhl.easy.echosite.cn/dthealth/web/DHCPE.Service.NetComService.CLS", "<Request><locId></locId></Request>");
	}
	
	/**
	 * 模拟soapUI调用   HIS服务地址
	 * @param method
	 * @param requestXml
	 * @param format
	 * @return
	 */
	public static Object invokeSoapUI(String url, String requestXml) {
		String rs=null;
		try {
			rs = getOpatientPaymentData(url,requestXml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs; 
	}
	/**
	 * SOAP UI 调用<br/>
	 */
	public static String getOpatientPaymentData(String url, String requestXml) throws Exception{
		String respondXML="";
		respondXML =soapSpecialConnection(requestXml,url);
		return respondXML;
	}
	
	
	
	/**
	 * @param questXML
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static String soapSpecialConnection(String questXML,String url) throws Exception {
		String soapHeader="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.dhcpe.org\">"
		   +"<soapenv:Header/>"
		   +" <soapenv:Body>"
		   +questXML
		   +"   </soapenv:Body>"
		   +"</soapenv:Envelope>";
		System.out.println("请求数据============="+soapHeader);
		String s = new String();
		URL u = new URL(url);
		HttpURLConnection conn = (HttpURLConnection) u.openConnection();
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setUseCaches(false);
		conn.setDefaultUseCaches(false);
		conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
		conn.setRequestProperty("SOAPAction", "");
		conn.setRequestMethod("POST"); // 定义输出流
		OutputStream output = conn.getOutputStream();
		if (null != soapHeader) {
			byte[] b = soapHeader.toString().getBytes("UTF-8"); // 发送soap请求报文
			output.write(b, 0, b.length);
		}
		output.flush();
		output.close(); // 定义输入流，获取soap响应报文
		InputStream input = conn.getInputStream(); // 需设置编码格式，否则会乱码
		s = IOUtils.toString(input, "UTF-8");
		input.close();
		
		String result = s.toString();//.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
		System.out.println("HIS的源数据为：" + result);
		String results = (String) result.subSequence(result.indexOf("<![CDATA[",1) + 9, result.lastIndexOf("]]>"));
		System.out.println("HIS返回参数:"+results);
		return results;	
	}
	
}
